//
//  ContractInfoDetailViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/15.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ContractInfoDetailViewController: PotentialDetailViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        button.removeFromSuperview()
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        self.page.view.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 55), size: CGSize.init(width: self.view.bounds.size.width, height: self.view.bounds.size.height - 55))
    }
    
    override func configTitles() -> Array<String> {
        return ["基本信息","关联信息","开票回款信息"]
    }
    
    override func configViewControllers() -> [UIViewController] {
        let scan = AddScanFilesViewController()
        scan.filePath = .Contract
        return [scan,BaseSectionedTableViewController(),TitleContentTableViewController()]
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json : JSON)->Void{
            if json.contents?["success"]?.bool == true {
                if let data = json.contents?["data"] {
                    let purchase = self.ctls[0] as! AddScanFilesViewController
                    var contents: Array<Dictionary<String,String?>> = []
                    contents.append(["合同名称": data.contents?["contractname"]?.rawString()])
                    contents.append(["合同编号": data.contents?["contractid"]?.rawString()])
                    contents.append(["K2编号": data.contents?["k2num"]?.rawString()])
                    let money = (data.contents?["signmoney"]?.int ?? 0) / 100
                    contents.append(["签订金额(元)": String(money).rmbformat])
                    contents.append(["合同分类": data.contents?["contracttypeName"]?.rawString()])
                    contents.append(["签订日期": data.contents?["signdata"]?.rawString()])
                    contents.append(["合同开始日期": data.contents?["statedata"]?.rawString()])
                    contents.append(["合同结束日期": data.contents?["enddata"]?.rawString()])
                    contents.append(["归属部门": data.contents?["projectInfo"]?.contents?["applyDept"]?.rawString()])
                    contents.append(["客户经理":data.contents?["customerManager"]?.rawString()])
                    contents.append(["客户单位":data.contents?["customerconpany"]?.rawString()])
                    contents.append(["客户单位联系人":data.contents?["customercompanycontact"]?.rawString()])
                    contents.append(["联系人电话":data.contents?["contactphone"]?.rawString()])
                    contents.append(["收入来源":data.contents?["sourcedatatmp"]?.rawString()])
                    if let ifprotected = data.contents?["whether"]?.bool {
                        if ifprotected == true {
                            contents.append(["是否质保金":"是"])
                            let warrantygold = (data.contents?["warrantygold"]?.int ?? 0) / 100
                            contents.append(["质保金(元)":String(warrantygold)])
                        } else {
                            contents.append(["是否质保金":"否"])
                            let warrantygold = (data.contents?["warrantygold"]?.int ?? 0) / 100
                            contents.append(["质保金(元)":String(warrantygold)])
                        }
                    } else {
                        contents.append(["是否质保金":"否"])
                        let warrantygold = (data.contents?["warrantygold"]?.int ?? 0) / 100
                        contents.append(["质保金(元)":String(warrantygold)])
                    }
                    contents.append(["收款方式":data.contents?["paymentMethodName"]?.rawString()])
                    contents.append(["收款方式说明":data.contents?["explain"]?.rawString()])
                    if let attachs = data.contents?["attachFiles"]?.array {
                        purchase.documents = attachs
                    }
                    if let scan = data.contents?["scanningAttachFiles"]?.array {
                        purchase.scanfiles = scan
                    }
                    
                    purchase.source = contents
                    
                    purchase.tableView.reloadData()
                    
                    let basesectioned = self.ctls[1] as! BaseSectionedTableViewController
                    basesectioned.sectiontitles = ["关联投标信息","关联项目信息"]
                    var section1: Array<Dictionary<String,String?>> = []
                    section1.append(["分标编号:": data.contents?["branchBidNumber"]?.rawString()])
                    section1.append(["包号:":data.contents?["packageNumber"]?.rawString()])
                    section1.append(["中标通知书编号:":data.contents?["noticeNumber"]?.rawString()])
                    
                    let bidAmount = (data.contents?["bidAmount"]?.int ?? 0) / 100
                    section1.append(["中标金额:":String(bidAmount).rmbformat])
                    section1.append(["中标日期:":data.contents?["dateOfNotice"]?.rawString()])
                    basesectioned.source.append(section1)
                    var section2: Array<Dictionary<String,String?>> = []
                    
                    section2.append(["项目编号:":data.contents?["projectInfo"]?.contents?["projectNumber"]?.rawString()])
                    section2.append(["项目名称:":data.contents?["projectInfo"]?.contents?["gsProjName"]?.rawString()])
                    section2.append(["所属部门:":data.contents?["projectInfo"]?.contents?["applyDept"]?.rawString()])
                    section2.append(["项目经理:":data.contents?["projectInfo"]?.contents?["projManagerName"]?.rawString()])
                    section2.append(["立项日期:":data.contents?["projectInfo"]?.contents?["setDate"]?.rawString()])
                    basesectioned.source.append(section2)
                    
                    basesectioned.tableView.reloadData()
                    let backinfo = self.ctls[2] as! TitleContentTableViewController
                    var contents1: Array<Dictionary<String,String?>> = []
                    let invoiceMoney = (data.contents?["invoiceMoney"]?.int ?? 0) / 100
                    contents1.append(["累计已开票(含税/元)": String(invoiceMoney).rmbformat])
                    let alreadyPaidMoney = (data.contents?["alreadyPaidMoney"]?.int ?? 0) / 100
                    contents1.append(["累计已回款(含税/元):":String(alreadyPaidMoney).rmbformat])
                    
                    contents1.append(["开票未回款(含税/元):":String(invoiceMoney - alreadyPaidMoney).rmbformat])
                    
                    contents1.append(["预收款(含税/元):":String(invoiceMoney - alreadyPaidMoney).rmbformat])
                    
                    if invoiceMoney > 0 {
                        let ticketbackratio = Double(alreadyPaidMoney) / Double(invoiceMoney) * 100
                        
                        contents1.append(["开票回款比例:":String(format: "%.2f%%", ticketbackratio)])
                    } else {
                        contents1.append(["开票回款比例:":"0.00%"])
                    }
                    
                    if money > 0 {
                        let contractratio = Double(alreadyPaidMoney) / Double(money) * 100
                        
                        contents1.append(["合同回款比例:": String(format: "%.2f%%", contractratio)])
                    } else {
                        contents1.append(["合同回款比例:": "0.00%"])
                    }
                    
                    backinfo.source = contents1
                    
                    backinfo.tableView.reloadData()
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        return suc
    }

    override func path(for name: String) -> String {
        return SaleContractDetail
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
